package com.yunxiang.summer.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.yunxiang.summer.activity.MainActivity;
import com.yunxiang.summer.R;

import java.util.List;

/**
 * 内层适配器：处理单条动态
 */
public class DynamicItemAdapter extends RecyclerView.Adapter<DynamicItemAdapter.ItemViewHolder> {
    private Context context;
    private List<MainActivity.Dynamic> dynamicList;

    public DynamicItemAdapter(Context context, List<MainActivity.Dynamic> dynamicList) {
        this.context = context;
        this.dynamicList = dynamicList;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_single_dynamic, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        MainActivity.Dynamic dynamic = dynamicList.get(position);

        // 设置动态内容
        holder.tvContent.setText(dynamic.getContent());
        holder.tvTime.setText(dynamic.getTime());

        // 动态添加图片
        holder.llImages.removeAllViews();
        int[] images = dynamic.getImageList();
        if (images != null && images.length > 0) {
            // 最多显示3张图片
            int displayCount = Math.min(images.length, 3);
            for (int i = 0; i < displayCount; i++) {
                ImageView imageView = new ImageView(context);
                // 设置图片布局参数
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        0,
                        dpToPx(100),
                        1.0f
                );
                int margin = dpToPx(4);
                params.setMargins(margin, 0, margin, 0);
                imageView.setLayoutParams(params);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setImageResource(images[i]);

                holder.llImages.addView(imageView);
            }
        }
    }

    @Override
    public int getItemCount() {
        return dynamicList.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvContent;
        LinearLayout llImages;
        TextView tvTime;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvContent = itemView.findViewById(R.id.tv_content);
            llImages = itemView.findViewById(R.id.ll_images);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }

    /**
     * dp转px工具方法
     */
    private int dpToPx(int dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density + 0.5f);
    }
}
